package huadi.algorithm.sort;


/**
 * Algorithm Util class
 *
 * @author Huadi
 */
public class SortingUtil {

    public static int[] intArray() {
        // return new int[] { 8, 4, 6, 3, 7, 1, 5, 2, 6 };
        return new int[]{90, 5, 23, 54, 87, 3, 66, 77, 77, 25, 43, 1};
        // return new int[] { 90, 66, 87, 25, 43, 77, 77, 5, 3, 23, 1, 54 };
    }

    public static void printArray(int[] array) {
        System.out.print("Values in array:\n{");
        for (int i = 0; i < array.length - 1; i++) {
            System.out.print(array[i] + ", ");
        }
        System.out.println(array[array.length - 1] + "}");
    }

    public static void swap(int[] array, int firstIndex, int lastIndex) {
        int temp = array[firstIndex];
        array[firstIndex] = array[lastIndex];
        array[lastIndex] = temp;
    }

    /**
     * This method is only for testing, runs lower than the swap(int[], int, int).
     *
     * @param array
     * @param firstIndex
     * @param lastIndex
     */
    @Deprecated
    public static void swapBit(int[] array, int firstIndex, int lastIndex) {
        array[firstIndex] = array[firstIndex] ^ array[lastIndex];
        array[lastIndex] = array[firstIndex] ^ array[lastIndex];
        array[firstIndex] = array[firstIndex] ^ array[lastIndex];
    }

    public static void main(String[] args) {
        int[] array = intArray();
        printArray(array);
        System.out.println("Sorting...");
        long startTime = System.nanoTime();
        HeapSort.sort(array);
        long endTime = System.nanoTime();
        System.out.println("Sort completed!");
        printArray(array);
        System.out.println("SortTime: " + (endTime - startTime) + " nanoseconds.");
    }
}
